package joe.beer;

import java.text.DecimalFormat;

import android.text.format.DateUtils;
import android.text.format.Time;

public class Compute {


	public Compute(){

	}
	
	public static String getTimeDifference(long now, long then){
        if(now > then)
            return DateUtils.formatElapsedTime((now - then)/1000L);
        else 
            return "";
    }
	
	// approximate .015 eliminated per hour
	// subtract .015 from BAC until under .08
	public static String timeUntilSober(Person p , double alcohol, long t)
	{
		double r = .66;
		double bac;
		double i = 0;
		String returnValue = "";
		double timeDifference = ( (double)(System.currentTimeMillis() - t) / (double)(60 * 1000 * 60));
		
		if (p.isMale())
			r = .73;
		
		bac = ((alcohol * 5.14)/((double)p.getWeight() * r)) - (.015 * timeDifference);
		
		while (bac > .08)
		{
			bac -= (.015 * .01);
			i += .01;
		}
		
		long elapsedSeconds = (long)(i * 60 * 60) ;
		
		returnValue = DateUtils.formatElapsedTime(elapsedSeconds);
		return returnValue ;
	}
	
	// %BAC = (A x 5.14/W x r) - .015 x H
	
	// "A" is the total number of liquid ounces of alcohol consumed 
	// wine ( 5oz x 12%) beer (12 oz * 5%) shot (1.25 oz x 40% )
	// "W" is weight in lbs
	// "r" is alcohol distribution ration .73 (men) or .66 (women)
	// "H" is the period in hours during which alcohol was eliminated
	
	public static String calculateBac(Person p , double alcohol, long t)
	{
		double r = .66;
		double bac = 0;
		String bacValue;
		double timeDifference =  ((double)(System.currentTimeMillis() - t) / (double)(60 * 1000 * 60));
		
		if (p.isMale())
			r = .73;
		
		bac = ((alcohol * 5.14)/((double)p.getWeight() * r)) - .015 * timeDifference;
		if (bac <= 0)
			bac = 0.001;
		DecimalFormat df = new DecimalFormat("#.##");
		if (bac > 0)
			bacValue = df.format(bac);
		else
			bacValue = "0.00";
		return bacValue;
	}

}
